from MovieLand import db
from datetime import datetime

# 电影表 与 分类表 的 联结关系表
moive_for_category_table = db.Table('moive_category',
                                    db.Column('movie_id', db.Integer, db.ForeignKey('movie.movie_id')),
                                    db.Column('category_id', db.Integer, db.ForeignKey('category.category_id')))

# 电影表 与 演员表的 联结关系表
moive_for_artist_table = db.Table('movie_artist',
                                  db.Column('movie_id', db.Integer, db.ForeignKey('movie.movie_id')),
                                  db.Column('artist_id', db.Integer, db.ForeignKey('artist.artist_id')))


class Movie(db.Model):
    __tablename__ = 'movie'

    movie_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(30), nullable=False)
    desc = db.Column(db.String(200), nullable=False)
    publish_year = db.Column(db.String(10), default='其他', nullable=True)
    detail_url = db.Column(db.String(100), nullable=True, default='')
    cover_img = db.Column(db.String(100), nullable=True, default='')
    unique_id = db.Column(db.Integer, db.ForeignKey('hashunique.id'))
    country_id = db.Column(db.Integer, db.ForeignKey('country.id'))
    
    unique = db.relationship('HashUnique')  # 绑定
    country = db.relationship('Country', backref='moive')

    artists = db.relationship('Artist',
                              secondary=moive_for_artist_table,
                              backref='movie',
                              lazy='dynamic')
    categorys = db.relationship('Category',
                                secondary=moive_for_category_table,
                                backref='moive',
                                lazy='dynamic')

    def __repr__(self):
        return '<- %s ->' % self.name


class Category(db.Model):
    """
    电影分类
    """
    __tablename__ = 'category'
    category_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(10), nullable=False)

    def __repr__(self):
        return '<Moive Category - %s>' % self.name


class Artist(db.Model):
    """
    演员艺人
    """
    # artist.movie
    __tablename__ = 'artist'
    artist_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(10), nullable=False)

    def __repr__(self):
        return '<Moive Aritst - %s>' % self.name


class Country(db.Model):
    """
    所属地区， 中国， 日本 ， 美国...
    """
    __tablename__ = 'country'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(10), nullable=False)

    def __repr__(self):
        return '<Moive Country - %s>' % self.name


class HashUnique(db.Model):
    """
    唯一moive标识： 用于去重
    """
    __tablename__ = 'hashunique'
    id = db.Column(db.Integer, primary_key=True)
    uniquetag = db.Column(db.String(100), nullable=False) # 电影名称的hash
    moive = db.relationship('Movie', uselist=False)

    def __repr__(self):
        return '<Moive hash %s>' % self.uniquetag
